﻿using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.Settings;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Builders;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;
using YesSql.Sql;
using Ruitu.Edu.TrainingDemo.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OrchardCore.Media.Fields;
using OrchardCore.Media.Settings;  
using Ruitu.Edu.TrainingDemo.Indexes;

using static Ruitu.Edu.TrainingDemo.Migrations.ContentFieldEditorEnums;

namespace Ruitu.Edu.TrainingDemo.Migrations
{
    public class PersonMigrations : DataMigration
    {
        private readonly IContentDefinitionManager _contentDefinitionManager;

        public PersonMigrations(IContentDefinitionManager contentDefinitionManager)
        {
            _contentDefinitionManager = contentDefinitionManager;
        }

        public async Task<int> CreateAsync()
        {
            // 配置 PersonPart 内容部件
            await _contentDefinitionManager.AlterPartDefinitionAsync(nameof(PersonPart), part => part
                .WithField(nameof(PersonPart.Biography), field => field
                .OfType(nameof(TextField))
                .WithDisplayName("Biography")
                .WithEditor(TextFieldEditors.TextArea)
                .WithSettings(new TextFieldSettings
                {
                    Hint = "Enter a brief biography.",
                }))
            );
            
            // 创建 PersonPage 内容类型
            await _contentDefinitionManager.AlterTypeDefinitionAsync("PersonPage", type => type
                 .Creatable()
                 .Listable()
                 .WithPart(nameof(PersonPart))
            );

            // 创建一个 widget
            await _contentDefinitionManager.AlterTypeDefinitionAsync("PersonWidget", type => type
                 .Stereotype("Widget")
                 .WithPart(nameof(PersonPart))
            );

            // 创建索引表
            await SchemaBuilder.CreateMapIndexTableAsync<PersonPartIndex>(table => table
                .Column<DateTime>(nameof(PersonPart.BirthDateUtc))
                .Column<int>(nameof(PersonPartIndex.Handedness))
                .Column<string>(nameof(PersonPartIndex.ContentItemId), col => col.WithLength(26))
            );

            return 1; // 返回迁移版本号
        }

        public async Task<int> UpdateFrom1Async()
        {
            // 更新 PersonPart 的 Avatar 字段
            await _contentDefinitionManager.AlterPartDefinitionAsync(nameof(PersonPart), part => part
                .WithField(nameof(PersonPart.Avatar), field => field
                .OfType(nameof(MediaField))
                .WithDisplayName("Avatar")
                .WithEditor(TextFieldEditors.IconPicker)
                .WithSettings(new MediaFieldSettings
                {
                    Hint = "Select an avatar image.",
                    AllowedExtensions = new[] { ".jpg",".jpeg", ".png" },
                    Multiple = false,
                    Required = false
                }))
            );
            return 2; // 返回迁移版本号
        }
    }


    public static class ContentFieldEditorEnums
    {
        [Description("Editor options for HtmlFields.")]
        public enum HtmlFieldEditors
        {
            Monaco = 0,
            Multiline = 1,
            Trumbowyg = 2,
            Wysiwyg = 3
        }
        [Description("Editor options for MarkdownFields. Wysiwyg editor can be used for MarkdownBodyParts as well.")]
        public enum MarkdownFieldEditors
        {
            TextArea = 0,
            Wysiwyg = 1
        }
        [Description("Editor options for TaxonomyFields.")]
        public enum TaxonomyFieldEditors
        {
            Tags = 0
        }
        [Description("Editor options for TextFields.")]
        public enum TextFieldEditors
        {
            CodeMirror = 0,
            Color = 1,
            Email = 2,
            IconPicker = 3,
            Monaco = 4,
            PredefinedList = 5,
            Tel = 6,
            TextArea = 7,
            Url = 8
        }
    }
}
